import 'package:flutter/material.dart';

class StarRating extends StatefulWidget {
  final double rating;
  final double maxRating;
  final int count;
  final double size;
  final Color unselect;
  final Color select;

  const StarRating(
      {required this.rating,
        this.maxRating = 10,
        this.count = 5,
        this.size = 30,
        this.unselect = Colors.grey,
        this.select = Colors.yellow});

  @override
  _StarRatingState createState() => _StarRatingState();
}

class _StarRatingState extends State<StarRating> {
  @override
  Widget build(BuildContext context) {
    return Stack(
      children: [
        Row(
          mainAxisSize: MainAxisSize.min,
          children: buildunselect(),
        ),
        Row(mainAxisSize: MainAxisSize.min,
          children: buildselect(),
        ),
      ],
    );
  }

  List<Widget> buildunselect() {
    return List.generate(widget.count, (index) {
      return Icon(
        Icons.star_border,
        color: widget.unselect,
        size: widget.size,
      );
    });
  }

  List<Widget> buildselect() {
    List<Widget> starts = [];
    final start =Icon(Icons.star,color: widget.select,size: widget.size,);
    double onevalue = widget.maxRating/widget.count;
    int entireCount = (widget.rating/onevalue).floor();
    for(int i=0;i<entireCount;i++){
      starts.add(start);
    }
    double leftwidth = ((widget.rating/onevalue)-entireCount) * widget.size;
    final halfStar = ClipRect(
      clipper: StartClipper(leftwidth),
      child: start,
    );
    starts.add(halfStar);
    print(starts.length);
    return starts;
  }
}

class StartClipper extends CustomClipper<Rect>{
  double width;

  StartClipper(this.width);

  @override
  getClip(Size size) {

    return Rect.fromLTRB(0, 0, 20, size.height);
  }

  @override
  bool shouldReclip(covariant CustomClipper oldClipper) {
    return false;
  }

}